<?php
namespace core;
use core\lib\conf;
use core\lib\log;

class sve
 {
    static public $classMap = [];
    static public function run() {

        $whoops = new \Whoops\Run;
        $whoops->prependHandler( new \Whoops\Handler\PrettyPageHandler );
        $whoops->register();

       

        //加载配置
        conf::load();

       p( conf::$arr );

        log::init();
        log::log( 'test' );

        $route = new \core\lib\route();

        $ctrlclass = $route::$ctrl;
        $action = $route::$action;

        // p( $ctrlclass );
        /**
        * 1.获取ctrl文件
        * 2.实例化 ctrl对像
        */
        $ctrlfile = SVEPHP.'/app/ctrl/'.$ctrlclass.'Ctrl.php';
        $ctrlclass = '\app\ctrl\\'.$ctrlclass.'Ctrl';
        if ( is_file( $ctrlfile ) ) {
            include  $ctrlfile;
            $ctrl = new $ctrlclass;

            if ( method_exists( $ctrl, $action ) ) {
                $ctrl->$action();
            } else {
                throw new \Exception( '没有找到方法'.$action );
            }

        } else {
            throw new \Exception( '没有找到控制器'.$ctrlclass );

        }
    }
    static public function load( $class ) {

        if ( isset( $classMap[$class] ) ) {
            return true;
        } else {
            $class = str_replace( '\\', '/', $class );
            $file = SVEPHP.'/'.$class.'.php';
            if ( \file_exists( $file ) ) {
                include $file;
                self::$classMap[$class] = $class;
            } else {
                return false;
            }
        }

    }
}
